package org.nViron.nvRender;

/**
 * Three dimensional plane
 * @author Roland Haring <br>
 * based on code by <a href="http://www.lighthouse3d.com/opengl/viewfrustum/">Lighthouse3d</a> <p>
 * <script type="text/javascript" src="http://www.ohloh.net/p/376434/widgets/project_basic_stats.js"></script>
 */
public class nvPlane {
    public nvVec3 normal = new nvVec3();
    public nvVec3 point  = new nvVec3();
    public float  d;

    public nvPlane() {}

    public nvPlane(nvVec3 v1, nvVec3 v2, nvVec3 v3) {
        set3Points(v1, v2, v3);
    }

    public float distance(nvVec3 p) {
        return (d + normal.dot(p));
    }

    public void set3Points(nvVec3 v1, nvVec3 v2, nvVec3 v3) {
        nvVec3 aux1 = new nvVec3();
        nvVec3 aux2 = new nvVec3();

        aux1   = v1.sub(v2);
        aux2   = v3.sub(v2);
        normal = aux2.cross(aux1);
        normal.normalize();
        point.copy(v2);
        d = -(normal.dot(point));
    }

    public void setCoefficients(float a, float b, float c, float d) {

        // set the normal vector
        normal.set(a, b, c);

        // compute the lenght of the vector
        float l = normal.length();

        // normalize the vector
        normal.set(a / l, b / l, c / l);

        // and divide d by th length as well
        this.d = d / l;
    }

    public void setNormalAndPoint(nvVec3 _normal, nvVec3 _point) {
        normal.copy(_normal);
        normal.normalize();
        d = -(normal.dot(_point));
    }
}



